
package com.island.practice.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.island.practice.entity.Person;
import com.island.practice.entity.Role;
import com.island.practice.entity.PersonRoleRelation;


/**
 * Servlet implementation class MaintainPersonServlet
 */
public class MaintainRoleServlet extends AbstractServlet {
	

	private static final String SQL_UPDATE_ROLE = "UPDATE ROLE r " +
		    			"SET r.ROLE_NAME = ? WHERE r.ROLE_ID = ? ";
	private static final String SQL_QUERY_ROLE = 
		"SELECT p.* FROM ROLE p WHERE ROLE_ID = ? ";
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String roleId = request.getParameter("roleId");
		
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		Role role = null;
		
		try {
			connection = this.getConnection();
		
			statement = connection.prepareStatement(SQL_QUERY_ROLE);
			statement.setString(1, roleId);
			resultSet = statement.executeQuery();
			
			if(resultSet.next()){
				role = new Role();
				role.setRoleId(resultSet.getString("ROLE_ID"));
				role.setRoleName(resultSet.getString("ROLE_NAME"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException("Maintain role(roleId:" + roleId + ")", e);
		}
		
		request.setAttribute("role", role);
		request.getRequestDispatcher("/role/MaintainRole.jsp").forward(request, response);
	}
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	String roleId = request.getParameter("roleId");
    	String roleName = request.getParameter("roleName");
    	
    	Connection connection = null;
    	PreparedStatement statement = null;
    	
    	try {
			connection = this.getConnection();
	    	connection.setAutoCommit(false);
	    	
	    	//1. update role
	    	statement = connection.prepareStatement(SQL_UPDATE_ROLE);
	    	
	    	statement.setString(1, roleName);
	    	statement.setString(2, roleId);
	    	statement.execute();

	    	
    	} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException("maintain for role(roleId:" + roleId + ") failed", e);
		} finally {
			closeStatement(statement);
			closeConnection(connection);
		}
        //Forward to the jsp page for rendering
        request.getRequestDispatcher("ListRole").forward(request, response);
	}

}
